package ipfms.stripes;

import able.stripes.JSP;

import com.google.inject.Inject;
import ipfms.dao.BuildingDAO;
import ipfms.manager.BuildingManager;
import ipfms.model.Building;
import ipfms.model.Pengguna;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;

@UrlBinding("/welcome")
public class WelcomeActionBean extends BaseActionBean {

    private Pengguna pengguna;
    private Building building;
    private List<Building> listOfBuilding;
    @Inject
    BuildingManager buildingManager;
    @Inject
    BuildingDAO buildingDAO;

    @DefaultHandler
    public Resolution welcome() {
        pengguna = getContext().getCurrentUser();
        listOfBuilding = new ArrayList<Building>();
        if (listOfBuilding == null || listOfBuilding.isEmpty()) {
            List<Building> listOfBuildingTmp = buildingDAO.findAll();
            if (listOfBuildingTmp != null && !listOfBuildingTmp.isEmpty()) {
                for (Building prop : listOfBuildingTmp) {
                    if (prop.getPublish() == 'Y') {
                        listOfBuilding.add(prop);
                    }
                }
            }
        }
        return new JSP("home.jsp");
    }

    public Resolution search() {
        return new JSP("search.jsp");
    }

    public Resolution searchFilter() {
        if (building != null) {
            listOfBuilding = buildingManager.searchByStateType(building);
        } else {
            listOfBuilding = new ArrayList<Building>();
            List<Building> listOfBuildingTmp = buildingDAO.findAll();
            if (listOfBuildingTmp != null && !listOfBuildingTmp.isEmpty()) {
                for (Building prop : listOfBuildingTmp) {
                    if (prop.getPublish() == 'Y') {
                        listOfBuilding.add(prop);
                    }
                }
            }
        }
        return new JSP("search_result.jsp");
    }

    public Resolution viewDetail() {
        String idBuilding = getContext().getRequest().getParameter("idBuilding");
        if (StringUtils.isNotBlank(idBuilding)) {
            building = buildingDAO.findById(Integer.valueOf(idBuilding));
        }
        return new JSP("property/property_detail.jsp");
    }

    public Pengguna getPengguna() {
        return pengguna;
    }

    public void setPengguna(Pengguna pengguna) {
        this.pengguna = pengguna;
    }

    public Building getBuilding() {
        return building;
    }

    public void setBuilding(Building building) {
        this.building = building;
    }

    public List<Building> getListOfBuilding() {
        return listOfBuilding;
    }

    public void setListOfBuilding(List<Building> listOfBuilding) {
        this.listOfBuilding = listOfBuilding;
    }
}
